package com;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/**
 * 深度整合工具类
 */
public abstract class DepthMergeUtil {

    static BigDecimal RATE_FACTOR = BigDecimal.valueOf(1000);
    /**
     * 整合深度
     * asks(卖价):将所有交易所的所有asks深度按价格 - 价格*费率 从低到高重新排序
     * bids(买价):将所有交易所的所有asks深度按价格 + 价格*费率 从高到底重新排序
     *
     * resps的symbol必须一致
     */
    public static BkPriceConDepth merge(Collection<BkPriceConDepth> resps) throws Exception {
        if (resps == null || resps.isEmpty()) {
            throw new IllegalArgumentException("resps mustn't be empty");
        }
        String symbol = null;
        List<DepthPrice> tAsks = new LinkedList<>();
        List<DepthPrice> tBids = new LinkedList<>();
        for (BkPriceConDepth resp : resps) {
            if (symbol == null) {
                symbol = resp.getSymbol();
            } else if (!symbol.equals(resp.getSymbol())) {
                throw new IllegalArgumentException("symbol of resps mustn't be identical");
            }
            List<DepthPrice> asks = resp.getAsks();
            List<DepthPrice> bids = resp.getBids();
            if (asks != null) {
                tAsks.addAll(asks);
            }
            if (bids != null) {
                tBids.addAll(bids);
            }

        }
        tAsks.sort(Comparator.comparing(DepthPrice::getPrice));
        tBids.sort(Comparator.comparing(DepthPrice::getPrice).reversed());
        return new BkPriceConDepth(symbol, System.currentTimeMillis(), tAsks, tBids);
    }
}
